package com.demo.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.reggie.common.R;
import com.demo.reggie.entity.Employee;
import com.demo.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登入方法
     * @param request 用来将登入结果保存Session中
     * @param employee 用来将数据接住
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //将页面返回的密码进行加密
        String password=employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //根据页面提交的用户名查询数据
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();//封装一个查询结果集
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //根据查询返回结果对情况进行分析
        if(emp==null){//查询无此人
            return R.error("登录失败");
        }if(!emp.getPassword().equals(password)){//密码错误
            return R.error("密码错误");
        }if(emp.getStatus()==0){//状态被禁用
            return  R.error("员工账号状态已禁用");
        }
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        log.info(employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateTime(LocalDateTime.now());
//        Long empId=(Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        //单个异常捕获
//        try{
//            employeeService.save(employee);
//        }catch (Exception e){
//            R.error("新增失败");
//        }
        employeeService.save(employee);

        return R.success("新增成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        System.out.println(page+""+pageSize+""+name);
        //封装查询条件
        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 雪花算法生成的id从客户端传回后端有精度缺失
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee){

//        employee.setUpdateTime(LocalDateTime.now());
//
//        Long empId=(Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> getByID(@PathVariable Long id){
        Employee employee=employeeService.getById(id);
        return R.success(employee);
    }
}
